In the previous article, we
had a quick overview of Spring Boot and its key features. In this article, we will learn how to
develop a simple Hello World REST API using Spring Boot. We use Maven to build this project since most IDEs
support it.
We will use the latest version of Spring boot and make sure that you have installed Java 8+ on your
machine.
Let's get started with our objective of what we will build?.
We’ll build a REST API that will accept HTTP GET requests at:
http://localhost:8080/hello-world
and respond with a response String "Hello World!"
Hello World!
Here is the complete pom.xml file for your reference:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.app</groupId>
<artifactId>springboot-first-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-first-app</name>
<description>Spring Boot First Application</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
From the above pom.xml, let's understand a few important spring boot dependencies and plugins.
All Spring Boot projects typically use spring-boot-starter-parent as the parent in pom.xml.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
</parent>
Parent Poms allow you to manage the following things for multiple child projects and modules:
We use spring-boot-starter-web dependency to develop RESTful web services.
This dependency provides a tomcat server as the default embedded container:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
The Spring Boot Maven plugin provides many convenient features:
The spring-boot-starter-test dependency will get automatically added to the Spring boot project for creating JUnit test cases but in this article, we are not going to focus on JUnit test cases.
Let's create a HelloWorldController class and the below code to it:
package com.springboot.first.app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
// GET HTTP Method
// http://localhost:8080/hello-world
@GetMapping("/hello-world")
public String helloWorld() {
return "Hello World!";
}
}
The below class SpringbootFirstAppApplication is the entry point that sets up the Spring Boot application.
The @SpringBootApplication annotation enables auto-configuration and component scanning.
package com.springboot.first.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootFirstAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstAppApplication.class, args);
}
}
@SpringBootApplication is a convenience annotation that adds all of the following annotations internally:
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
From your IDE, run the SpringbootFirstAppApplication.main() method as a standalone Java class that will start the embedded Tomcat server on port 8080 and point the browser tohttp://localhost:8080/.
Just go to the root directory of the application and type the following command to run it -
$ mvn spring-boot:run
The application will start at Spring Boot’s default tomcat port 8080.
Just hit this link in a browser:. You will be able to see the response of this REST API in the browser.
In the next tutorial, we will discuss how to create a standard spring boot project structure or packaging structure.
Learn more complete Spring Boot on Spring Boot Tutorial